﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Dukou.Example.DTO.Domain;
using Dukou.Example.DTO.DTO;
using Dukou.DTO;

namespace Dukou.Example.DTO.Parsers
{
    public class CustomerParser : ObjectParser<CustomerDTO, Customer>
    {
        protected override CustomerDTO InternalParse(Customer k, IDictionary<string, ObjectCache> temp, int depth)
        {
            CustomerDTO dto = new CustomerDTO()
            {
                Id = k.Id,
                LocalId = Guid.NewGuid(),
                CreateTime = k.CreateTime,
                CustomerName = k.CustomerName,
                UpdateTime = k.UpdateTime
            };

            temp.Add(k.GetIdentity(), new ObjectCache(depth, dto));

            dto.Creator = ObjectParser.Parse<UserDTO, User, UserParser>(k.Creator, temp, depth - 1);
            dto.Updator = ObjectParser.Parse<UserDTO, User, UserParser>(k.Updator, temp, depth - 1);
            dto.MemberCards = ObjectParser.Parse<MemberCardDTO, MemberCard, MemberCardParser>(k.MemberCards, temp, depth - 1);

            return dto;
        }

        protected override Customer InternalParse(CustomerDTO t, IDictionary<string, ObjectCache> temp, int depth)
        {
            Customer customer = new Customer()
            {
                Id = t.Id,
                CreateTime = t.CreateTime,
                CustomerName = t.CustomerName,
                UpdateTime = t.UpdateTime
            };

            temp.Add(t.GetIdentity(), new ObjectCache(depth, customer));

            customer.Creator = ObjectParser.Parse<UserDTO, User, UserParser>(t.Creator, temp, depth - 1);
            customer.Updator = ObjectParser.Parse<UserDTO, User, UserParser>(t.Updator, temp, depth - 1);
            customer.MemberCards = ObjectParser.Parse<MemberCardDTO, MemberCard, MemberCardParser>(t.MemberCards, temp, depth - 1);

            return customer;
        }
    }
}
